package com.aaa.controller;


import com.aaa.entity.Message;
import com.aaa.entity.SysDeptEntity;
import com.aaa.service.DeptService;
import com.aaa.service.DeptServiceImpl;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.Map;

@WebServlet(name = "DeptServlet",urlPatterns = "/dept.action")
public class DeptServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");

       String method=request.getParameter("method");
       if (method!=null){
           switch (method){
               //查询
               case "query":
                   query(request,response);
                   break;
               case "add":
                   dept_add(request,response);
                   break;
               case"queryOne":
                   queryOne(request,response);
                   break;
               case "delete":
                   delete(request,response);
                   break;
           }
       }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      doPost(request,response);
    }

    /**
     * 查询父级校区
     * @param request
     * @param response
     * @throws IOException
     */
    private  void query(HttpServletRequest request, HttpServletResponse response)throws IOException{
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");

        String parent_id=request.getParameter("parent_id");
        int pid=parent_id==null?0:Integer.parseInt(parent_id);
        DeptService service=new DeptServiceImpl();
        List list=service.query(pid);
        String str= JSON.toJSONStringWithDateFormat(list,"yyyy-MM-dd");
        response.getWriter().print(str);
    }

    /**
     * 根据dept_id查询单个
     * @param request
     * @param response
     * @throws IOException
     */
    private  void queryOne(HttpServletRequest request, HttpServletResponse response)throws IOException{
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");

        String dept_id=request.getParameter("dept_id");
        int pid=dept_id==null?0:Integer.parseInt(dept_id);
        DeptService service=new DeptServiceImpl();
        Map map=service.queryOne(pid);
        String str=JSON.toJSONStringWithDateFormat(map,"yyyy-MM-dd");
        response.getWriter().print(str);
    }


    /**
     * 根据父类进行添加
     * @param request
     * @param response
     * @throws IOException
     */
    private  void dept_add(HttpServletRequest request, HttpServletResponse response)throws IOException{

        String parent_id=request.getParameter("parent_id");
        int pid=parent_id==""?0:Integer.parseInt(parent_id);
        String dept_id=request.getParameter("dept_id");
        int did=dept_id==""?0:Integer.parseInt(dept_id);
        String dept_name=request.getParameter("dept_name");

        String leader=request.getParameter("leader");
        String phone=request.getParameter("phone");
        String email=request.getParameter("email");

        SysDeptEntity dept=new SysDeptEntity();
        dept.setDeptId(did);
        dept.setParentId(Long.valueOf(pid));
        dept.setDeptName(dept_name);

        dept.setLeader(leader);
        dept.setPhone(phone);
        dept.setEmail(email);
        dept.setCreateBy("admin");
        dept.setCreateTime(new Date());
        dept.setUpdateBy("admin");
        dept.setUpdateTime( new Date());

        Message message=new Message();
        DeptService service=new DeptServiceImpl();
        message=service.dept_update(dept);
        String str=JSON.toJSONStringWithDateFormat(message,"yyyy-MM-dd");
        response.getWriter().print(str);
    }

    private  void delete(HttpServletRequest request, HttpServletResponse response)throws IOException{
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");

        String dept_id=request.getParameter("dept_id");
        int pid= Integer.parseInt(dept_id);
        DeptService service=new DeptServiceImpl();
        int result=service.dept_delete(pid);
        String str=JSON.toJSONString(result);
        response.getWriter().print(str);
    }
}
